package king.dominic.jlibrary.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import king.dominic.jlibrary.R;
import king.dominic.jlibrary.util.FragmentFactory;
import king.dominic.jlibrary.view.WindowUtil;

/**
 * Created by king on 2017/3/31.
 * OriginFragmentActivity
 * 要对Fragment传递参数，在super.onCreate(savedInstanceState)之前设置给Intent就可以
 * 也可以运行完super.onCreate(savedInstanceState)之后直接给fragment设置
 *
 * @see FragmentFactory
 */

public abstract class OriginFragmentActivity extends OriginActivity {
    protected Fragment fragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindowAttribute(windowAttribute);
        setContentView(R.layout.layout_framelayout);
        initFragment();
    }

    protected void initFragment() {
        Class<? extends Fragment> clazz = getFragmentClass();
        setFragment(FragmentFactory.obtain(this, clazz));
    }

    protected void initWindowAttribute(WindowUtil.WindowAttribute windowAttribute) {

    }

    public void setFragment(@NonNull Fragment fragment) {
        this.fragment = fragment;
        Bundle extras = getIntent().getExtras();
        if (extras != null && !extras.isEmpty()) this.fragment.setArguments(extras);
        getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
    }

    @NonNull
    protected abstract Class<? extends Fragment> getFragmentClass();

}
